package com;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import com.vo.Apple;

public class Demo {

	/**
	 * 函数式编程
	 * 
	 * @param inventory
	 * @param p
	 * @return
	 */
	public static List<Apple> filterApples(List<Apple> inventory, Predicate<Apple> p) {
		List<Apple> result = new ArrayList<>();
		for (Apple apple : inventory) {
			if (p.test(apple)) {
				result.add(apple);
			}
		}
		return result;
	}

	public static void sort() {
		List<Integer> numList = new ArrayList<>(Arrays.asList(10, 50, 5, 200, 9, 25));
		List<Apple> applelist = new ArrayList<Apple>() {
			{
				add(new Apple(10, false));
				add(new Apple(50, true));
				add(new Apple(5, false));
				add(new Apple(400, true));
				add(new Apple(9, false));
			}
		};
		// 常规升序
		numList = numList.stream().sorted().collect(Collectors.toList());
		// 常规逆序
		numList = numList.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
		// 根据重量升序
		List<Apple> applelist1 = applelist.stream().sorted(Comparator.comparing(Apple::getWight))
				.collect(Collectors.toList());
		// 根据重量逆序
		List<Apple> applelist2 = applelist.stream().sorted(Comparator.comparing(Apple::getWight).reversed())
				.collect(Collectors.toList());
		// lumbda排序
		Collections.sort(applelist, (e1, e2) -> {
			return e1.getWight() - e2.getWight();
		});

	}

}
